package servlet;

import model.Order;
import model.User;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.List;

@WebServlet(name = "my_orders", urlPatterns = "/my_orders")
public class MyOrdersServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orService = new OrderService();
        User user = (User) request.getSession().getAttribute("user");
        //登陆后直接获取对应用户的订单
        List<Order> list_buy = orService.selectAll(user.getId());
        List<Order> list_sell = orService.selectAllBySellerId(user.getId());
        List<Order> allOrders = new ArrayList<>();
        if (list_buy != null) allOrders.addAll(list_buy);
        if (list_sell != null) allOrders.addAll(list_sell);
        //将获取的list表格通过orderList字段传给前端
        request.getSession().setAttribute("orderListAll", allOrders);
        request.getSession().setAttribute("orderListBuy", list_buy);
        request.getSession().setAttribute("orderListSell", list_sell);
        request.getRequestDispatcher("/order_list.jsp").forward(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


}